/*
 * @Author: taoyuqing
 * @Date: 2022-12-21 21:23:07
 * @Description: 工具类
 */
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:spree/widgets/common_container.dart';
import 'package:spree/widgets/vip_content_box.dart';
import 'package:spree/widgets/vip_rectangle_box.dart';

class Utils {
  Utils._internal();
  factory Utils() => _instance;
  static late final Utils _instance = Utils._internal();
  // 提示框
  static showStatusDialog(BuildContext context,
      {bool success = false,
      String text = '',
      bool showCloseBtn = false,
      bool barrierDismissible = true}) {
    showDialog(
        barrierDismissible: barrierDismissible,
        context: context,
        builder: (BuildContext context) {
          return Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              VipRectangleBox(
                color: Color(0xFF140E37),
                width: 300,
                padding: EdgeInsets.all(20),
                child: Column(
                  children: [
                    VipRectangleBox(
                      width: double.infinity,
                      padding: EdgeInsets.fromLTRB(0, 40, 0, 40),
                      color: Color(0xFF291F62),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Image.asset(
                            success
                                ? 'images/success-icon.png'
                                : 'images/fail-icon.png',
                            width: 90,
                            height: 90,
                          ),
                          Padding(
                            padding: EdgeInsets.only(top: 20),
                            child: Text(
                              text,
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                  fontSize: 24,
                                  fontWeight: FontWeight.w700,
                                  color: Colors.white),
                            ),
                          )
                        ],
                      ),
                    ),
                    showCloseBtn?Container(
                      margin: EdgeInsets.only(top: 20),
                      child: ElevatedButton(
                        style: ButtonStyle(
                          backgroundColor:
                              MaterialStateProperty.all(Color(0xFF6757C0)),
                        ),
                        onPressed: () {
                          Navigator.pop(context);
                        },
                        child: Text(
                          'close',
                          style: TextStyle(fontSize: 16),
                        ),
                      ),
                    ):Container()
                  ],
                ),
              ),
            ],
          );
        });
  }

  // 深拷贝数组
  List copyWithList(List list) {
    List copyList = [];
    for (var item in list) {
      if (item is Map) {
        copyList.add(Map.from(item));
      } else if (item is List) {
        copyList.add(copyWithList(item));
      } else {
        copyList.add(item);
      }
    }
    return copyList;
  }

  // 获取宽度
  double getScreenWidth(BuildContext context) {
    return MediaQuery.of(context).size.width;
  }

  double getScreenHeight(BuildContext context) {
    return MediaQuery.of(context).size.height;
  }

  // 获取状态栏高度
  double getAppStatusBarHeight(BuildContext context) {
    return MediaQuery.of(context).padding.top;
  }

  // 获取本地图片的信息
  Future getLocalImageSize({required Image img}) async {
    Completer c = Completer();
    img.image.resolve(ImageConfiguration()).addListener(ImageStreamListener(
      (ImageInfo imageInfo, bool synchronousCall) {
        c.complete(imageInfo.image);
      },
    ));
    return c.future;
  }

  // 自定义主题色 0xFF1E164D
  MaterialColor createMaterialColor({Color? color}) {
    Color themeColor = color ?? Color(0xFF1E164D);
    List strengths = <double>[.05];
    Map<int, Color> swatch = {};
    final int r = themeColor.red, g = themeColor.green, b = themeColor.blue;

    for (int i = 1; i < 10; i++) {
      strengths.add(0.1 * i);
    }
    strengths.forEach((strength) {
      final double ds = 0.5 - strength;
      swatch[(strength * 1000).round()] = Color.fromRGBO(
        r + ((ds < 0 ? r : (255 - r)) * ds).round(),
        g + ((ds < 0 ? g : (255 - g)) * ds).round(),
        b + ((ds < 0 ? b : (255 - b)) * ds).round(),
        1,
      );
    });
    return MaterialColor(themeColor.value, swatch);
  }

  // 白色
  Color whiteColor() {
    return Color(0xFFFFFFFF);
  }

  // 边框的container
  Border loutLine() {
    return Border.all(width: 1, color: Color.fromRGBO(255, 255, 255, 0.5));
  }
}
